package com.supply.dl.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.TransGoodsBean;
import com.supply.dl.bean.request.TransGoodsRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.TakeDepotAdapter;

import java.util.List;

/**
 * 移库收货列表页面
 */
public class TakeDeliveryActivity extends BaseActivity
{

	/**
	 * 搜索内容输入框
	 */
	@BindView(R.id.common_search_main_searchContentEdt) EditText editSearch;

	/**
	 * 搜索按钮
	 */
	@BindView(R.id.common_search_main_searchTv) TextView txtSearch;

	@BindView(R.id.layout_load_failed) LoadTipFailedLayout loadTipFailedLayout;
	/**
	 * 列表显示
	 */
	@BindView(R.id.swipe_target) RecyclerView recyclerView; //修改了id
	/**
	 * 将要搜索的内容
	 */
	private String searchContent;
	/**
	 * 适配器
	 */
	private TakeDepotAdapter takeDepotAdapter;

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_change_depot_take_delivery;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.transfer_warehouse_receive_goods));
		loadTipFailedLayout.setRestartOnClickListener(this);
		showLeftBack();
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		txtSearch.setOnClickListener(this);
		trySearch();
	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{
		case R.id.common_search_main_searchTv:
			trySearch();
			break;
		case R.id.txt_load_failed:
			trySearch();
			break;
		}
	}

	/**
	 * 尝试搜索
	 */
	private void trySearch()
	{
		loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
		searchContent = editSearch.getText().toString().trim();
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		TransGoodsRequest transGoodsRequest = new TransGoodsRequest();
		transGoodsRequest.key = searchContent;
		httpManger.getTransGoodsList(transGoodsRequest);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				List<TransGoodsBean> transGoodsBeanList = (List<TransGoodsBean>) baseInfo.getData();
				if (transGoodsBeanList.size() > 0)
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
					takeDepotAdapter = new TakeDepotAdapter(TakeDeliveryActivity.this, transGoodsBeanList);
					recyclerView.setAdapter(takeDepotAdapter);
				}
				else
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
				}
			}

			@Override public void onFailure(String message)
			{
				showToast(message);
				loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
			}
		});
	}
}
